package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.utils.DateUtils;
import com.goods.common.vo.business.HealthVO;

import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;


/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/27 14:01
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class HealthServiceImpl implements HealthService {
    @Autowired
    private HealthMapper healthMapper;

    @Override
    public HealthVO isReport(Long id) {
        Example example = new Example(Health.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",id);
        criteria.andBetween("createTime", DateUtils.getDate(),DateUtils.nextDay(1));

        Health health = healthMapper.selectOneByExample(example);
        HealthVO healthVO = null;
        if(health!=null){
            healthVO = new HealthVO();
            BeanUtils.copyProperties(health,healthVO);
        }
        return healthVO;
    }

    @Override
    public void report(HealthVO healthVo) {
        Health health = new Health();
        BeanUtils.copyProperties(healthVo,health);
        health.setCreateTime(new Date());
        healthMapper.insert(health);
    }

    @Override
    public PageInfo<Health> history(String pageNum, String pageSize) {
        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<Health> healthList = healthMapper.selectAll();
        return new PageInfo<Health>(healthList);
    }
}
